package com.fzs.sshd.cli.server.channel;

import com.fzs.sshd.util.AdbParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.common.util.threads.CloseableExecutorService;
import org.apache.sshd.server.forward.TcpForwardingFilter;
import org.apache.sshd.server.forward.TcpipServerChannel;

import java.io.IOException;
import java.util.List;

@Slf4j
public class LoggingTcpipServerChannel extends TcpipServerChannel {


    public LoggingTcpipServerChannel(TcpForwardingFilter.Type type, CloseableExecutorService executor) {
        super(type, executor);
    }

    @Override
    public void handleData(Buffer buffer) throws IOException {
        // 拦截客户端发来的数据
        int len = buffer.available();      // 可用字节数
        int pos = buffer.rpos();           // 读取指针当前位置
        byte[] data = new byte[len];
        System.arraycopy(buffer.array(), pos, data, 0, len);

        List<AdbParser.AdbMessage> parse = AdbParser.parse(data);
        for (AdbParser.AdbMessage msg : parse) {
            List<String> commands = AdbParser.extractCommands(msg);
            for (String command : commands) {
                log.info("客户端操作类型:{},命令:{}", msg.commandString(), command);
            }
        }
        super.handleData(buffer);
    }



}
